package cn.com.elite;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.com.elite.util.Util;

public class EliteStartThread extends HttpServlet{
	private static final Log log=LogFactory.getLog(EliteStartThread.class);
	public static List thread_list;
	public static String JUDGE_FORMAT;
	public static String FORMAT_SIGN;
	public static Properties readFile(String address){
		String fileName=address;
		InputStream in=null;  
        Properties pros=new Properties();  
        try{
        	if(null!=fileName){ 
        		in=new BufferedInputStream(new FileInputStream(fileName));
        		pros.load(in);
        		return pros;
        	}
        }catch(Exception e){
        	log.error("读取资源文件出错:"+Util.getERRO(e));  
        }finally{
        	try{
        		if(null!=in){
        			in.close(); 
        		}
        	}catch(Exception e){
        		log.error("关闭流失败:"+Util.getERRO(e));  
        	}
        	return pros;
        }
	}
	public void get_init_param(){
		try{
			URL a=this.getClass().getResource("eliteforward.properties");
			String url=a.toString();
			url=url.substring(6,url.length());
			Properties pros=readFile(url);
			if(pros!=null){
				JUDGE_FORMAT=pros.getProperty("JUDGE_FORMAT");
				FORMAT_SIGN=pros.getProperty("FORMAT_SIGN");
			}
		}catch(Exception e){
			log.error("get_init_param()失败:"+Util.getERRO(e));
		}
	}
	public void init(){
		get_init_param();
		thread_list=new ArrayList();
		String ADDRESS=this.getInitParameter("ADDRESS");
		String Hz=this.getInitParameter("Hz");
		String trytime=this.getInitParameter("trytime");
		int HZ1=Integer.parseInt(Hz);
		int trytime1=Integer.parseInt(trytime);
		ServletContext application;   
        WebApplicationContext wac;   
        application = getServletContext();   
        wac=WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context  
        cn.com.elite.EliteForBean ebean=(cn.com.elite.EliteForBean)wac.getBean("ebean");      
        EliteThresd.setEbean(ebean);
		EliteThresd.setHz(HZ1);
		EliteThresd.setPOST_URL123(ADDRESS);
		EliteThresd.setTrytime(trytime1);
		String thread_num=this.getInitParameter("thread_num");
        int thread_num1=Integer.parseInt(thread_num);
        for(int i=0;i<thread_num1;i++){
        	thread_list.add(Collections.synchronizedMap(new HashMap()));
        }
		for(int i=0;i<thread_list.size();i++){
			EliteThresd td=new EliteThresd();
			td.setList((Map)thread_list.get(i));
			Thread t = new Thread(td); 
	        t.start(); //   启动任务
		}
	}

}
